package com.example.acceledemo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

public class IOManager {
	
	static String mDirName="MyKML";
	
	
	public static void writeStringAsFile(String string, String name, String dir) {
		
		File path = Environment.getExternalStorageDirectory();
		File directory = new File(path+File.separator+dir);
		if(!directory.exists())
			directory.mkdir();
		
		File out_plik = new File(name);
		FileWriter fileWriter;
		Log.d("MyIOExeption", "IOManager::path " + path + File.separator + dir + File.separator + out_plik);
		try {
			fileWriter = new FileWriter(path + File.separator + dir + File.separator + out_plik);
			BufferedWriter zapis = new BufferedWriter(fileWriter);
			zapis.write(string);
			zapis.close();
		} catch (IOException e) {
			Log.e("MyIOExeption", "IOExeption: IOManager:writeStringAsFile");
		}
	}
	
	public static String readFileAsString(String plik_in) {
		
		StringBuffer buffer =new StringBuffer();
		File path = Environment.getExternalStorageDirectory();
		
		File in_plik = new File(path + File.separator + mDirName +File.separator + plik_in);
		FileReader fileReader;
		try {
			fileReader = new FileReader(in_plik);
			BufferedReader odczyt = new BufferedReader(fileReader);
			String line = odczyt.readLine();
			
			while(line!=null)
			{
				buffer.append(line +"\n");
				
				line=odczyt.readLine();
			}
			
			odczyt.close();
		} catch (IOException e) {
			Log.d("MyIOExeption", "IOManager:readFileAsString");
		}

		return buffer.toString();
	}
	
	
	
	
	/*	public static Route readObject(String name) {
	File path = Environment.getExternalStorageDirectory();
	Route r = null;
	try {
		FileInputStream fileIn = new FileInputStream(path + File.separator
				+ mDirName + File.separator + name);
		ObjectInputStream in = new ObjectInputStream(fileIn);
		r = (Route) in.readObject();
		in.close();
		fileIn.close();
	} catch (IOException i) {
		Log.e("MyIOExeption", "IOExeption: IOManager:readObject");
		return null;
	} catch (ClassNotFoundException c) {
		Log.e("MyIOExeption", "ClassNotFoundException: IOManager:readObject");
		return null;
	}
	return r;

}

public static void writeObject(Route r, String name) {
	File path = Environment.getExternalStorageDirectory();
	try {
		FileOutputStream fileOut = new FileOutputStream(path
				+ File.separator + mDirName + File.separator + name);
		ObjectOutputStream out = new ObjectOutputStream(fileOut);
		out.writeObject(r);
		out.close();
		fileOut.close();
	} catch (IOException i) {
		Log.e("MyIOExeption", "IOExeption: IOManager:writeObject");
	}

}*/
	
}
